package servlet;

import service.ClassService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 8:12 下午 2021/6/6
 */
@WebServlet(name = "DeleteClassServlet", urlPatterns = "/deleteClass")
public class DeleteClassServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s = request.getParameter("classNo").trim();
        int classNo = Integer.parseInt(s);
        ClassService classService = new ClassService();
        //查询班级下是否还有学生
        boolean isEmpty = classService.isEmptyClass(classNo);
        System.out.println(isEmpty);
        if (isEmpty == true) {
            request.getSession().setAttribute("isEmpty","true");
            boolean isSuccess = classService.deleteClassService(classNo);
            if (isSuccess == true) {
                request.getSession().setAttribute("isDeleteClass", true);
            }
        } else {
            System.out.println("班级中还有其余学生，不能删除！");
            request.getSession().setAttribute("isEmpty","false");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
